package com.dempsey.modules.file.service;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.dempsey.common.cos.service.CosImageService;
import com.dempsey.modules.file.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * @author Dempsey
 * Date 2022/5/20
 * Description
 */
@Service
public class FileService {

    private final static Log log = LogFactory.get();

    @Autowired
    private CosImageService cosImageService;

    public String upload(File file) {
        // 重命名文件：日期，文件名hash，后缀
        String fileName = FileUtil.extractFilename(file.getName());

        String uploadFilePath = cosImageService.uploadFile(file, fileName);
        log.info("图片上传成功，路径：{}", uploadFilePath);

        file.delete();

        return uploadFilePath;
    }

    public String uploadHeadImg(File file) {
        // 重命名文件：日期，文件名hash，后缀
        String fileName = FileUtil.extractFilename(file.getName());
        File newFile = new File(fileName);
        FileUtil.scaleHeadImg(file, newFile);

        String uploadFilePath = cosImageService.uploadFile(newFile, fileName);
        log.info("图片上传成功，路径：{}", uploadFilePath);

        file.delete();
        newFile.delete();

        return uploadFilePath;
    }
}
